home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / bfmt139.zip / BFMT.CPP next >
C/C++ Source or Header  |  1993-04-28  |  22KB  |  760 lines

  1.  
  2. /*
  3.     want to edit stdin and output stdout
  4.     output either justified or left aligned or right aligned
  5.     centered or adjusted (c++ mode)
  6.     user specified width (of character field only), inherit
  7.     word spacing, all lines as paragraphs, specify spacing etc
  8.  
  9.     bfmt -fl -w40
  10.  
  11.  
  12.     plan :
  13.         get line of input and next (flag if no more)
  14.         if new para reset whitespace
  15.         else separate into words and spaces
  16.         pass words and spaces to format type
  17.  
  18.         format type :
  19.             arrange words and spaces
  20.             output to cout
  21.  
  22.  
  23. history :
  24.         1.33 bug in editing paragraph with ^Z on last line
  25.         13/4/93 fixed in version 1.34
  26.  
  27.     15/4/93 fixed bug that deleted words if paragraph too big
  28.         in version 1.37
  29.  
  30.         18/4/93 make last line in justified paragraph unfmt. (1.38)
  31.  
  32.     27/4/93 tried to convert to SunOS c++ ,re suggestion Yan Xu
  33.  
  34.     28/4/93 tested, done and improved by  Yan Xu
  35.         slight modifications by me (1.39)
  36.         to fix error in max_words_per_line
  37. */
  38.  
  39. // --------------------------------------------------------------
  40.  
  41. #include <time.h>
  42. #include <ctype.h>
  43. #include <iostream.h>
  44. #include <fstream.h>
  45. #include <iomanip.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. #define VERSION "1.39"
  50. #define MAX_LINE_LEN 160
  51. #define MAX_NUM_WORDS 80
  52. #define MAX_WORD_SZ   80
  53. #define WNUM_OFFSET   30
  54. #define MIN_WIDTH     10
  55. #define MAX_SP        80
  56.  
  57. // ------------------------------------ struct typedefs
  58.  
  59. typedef struct {
  60.     char   searchkey[20];
  61.     char   format[3];
  62.     char   pmarkers[10];
  63.     char   smarkers[10];
  64.     int    min;
  65.     int    paraspace;
  66.     int    keep;
  67.     int    text;
  68.     int    inherit;
  69.     int    allpara;
  70.     int    width;
  71.     } frmt;
  72.  
  73. frmt Fmt = {
  74.     "@#$%^Blb)8",
  75.             // searchkey
  76.     "l",
  77.             // left aligned
  78.     "./",
  79.             // para markers
  80.     ".?!*",
  81.             // sentence markers
  82.     4,
  83.         // minimum number of words to format justify
  84.     8,
  85.             // paragraph spacing
  86.     1,
  87.         // keep text directed formatting info
  88.     1,
  89.             // obey text directed formatting
  90.     1,
  91.             // space inherited
  92.     0,
  93.             // all lines as paragraphs
  94.     72 };
  95.             // width
  96.  
  97. typedef struct {
  98.     char    space[ MAX_LINE_LEN ];
  99.     char    param[ MAX_LINE_LEN];
  100.     int     lspace;
  101.     int     wnum;
  102.     int     restspace;
  103.     char    words  [MAX_NUM_WORDS] [MAX_WORD_SZ];
  104.     int     newp;
  105.     int     oldallpara;
  106.     int     oldinherit;
  107.     int     end;
  108.     } wdinfo;
  109.  
  110. wdinfo  Words = {
  111.     "",
  112.     "",
  113.     0,
  114.     0,
  115.     0,
  116.     "",
  117.     1,
  118.     0,
  119.     1,
  120.     0 } ;
  121.  
  122. char    buffer [MAX_LINE_LEN] = "";
  123.  
  124. // --------------------------------------------------------------
  125. int PUsage(void) {
  126.     cout << "\nText formatting version " << VERSION << endl;
  127.     cout << "send donations to Brendan Babb \n";
  128.     cout << "                  @ microbiol dept,";
  129.     cout << " Univ. Ave, UCT, Rondebosch 7700, RSA\n";
  130.     cout <<
  131. "\nUSAGE:  BFMT [options listed below]\n"
  132. "   [<{input}] [>{output}]  where input and output are filenames\n"
  133. "   [-f{type}] types are l,r,j,c,u,a\n"
  134. "   [-i{0|1}]  inherit whitespace   [-s{width}]  whitespace width\n"
  135. "   [-a{0|1}]  all lines are paragraphs\n"
  136. "   [-t{0|1}]  obey options in text       \n"
  137. "   [-k{0|1}]  keep options in text\n"
  138. "   [-({sentence delimiters}]          [-{{paragraph delimiters}]\n"
  139. "   [-w{width}]  right margin\n"
  140. "   [-m{min}]  minimum words to justify\n"
  141. "   [/u]  save previous options to default\n"
  142. "   [?|-h|-?]  display this and current defaults\n";
  143.  
  144.     return 0;
  145.     }
  146.  
  147. // --------------------------------------------------------------
  148.  
  149. int PValues()  {
  150.     cout << "\nCurrent default values :\n";
  151.     cout << setw(28) << "sentence markers " << setw(6)
  152.      <<  Fmt.smarkers;
  153.     cout << setw(28) << "paragraph markers " << setw(6)
  154.      << Fmt.pmarkers << endl;
  155.     cout << setw(28) << "paragraph spacing " << setw(6)
  156.      << Fmt.paraspace;
  157.     cout << setw(28) << "all lines as para " << setw(6)
  158.      << Fmt.allpara << endl;
  159.     cout << setw(28) << "keep param lines "  << setw(6) << Fmt.keep;
  160.     cout << setw(28) << "obey param lines "  << setw(6)
  161.      << Fmt.text << endl;
  162.     cout << setw(28) << "format " << setw(6) << Fmt.format;
  163.     cout << setw(28) << "whitespace inhertited " << setw(6)
  164.      << Fmt.inherit << endl;
  165.     cout << setw(28) << "width " << setw(6) << Fmt.width;
  166.     cout << setw(28) << "minimum words to fmt " << setw(6)
  167.      << Fmt.min << endl;
  168.     return 0;
  169.     }
  170.  
  171.  
  172. // -----------------------------------  work horses
  173.  
  174. /*
  175.     divide must divide the input buffer into as many words as it
  176.     can handle, until no more input, or until max words
  177.     or only one line if allpara
  178.     returning 1 prevents more input
  179.     its function is to set up global variables Word and buffer for
  180.     the work to be done
  181.  
  182.     if new para, must quit, the format functions
  183.     will then organize the words and spaces as need be
  184.     to check for new para must look at string of paramarkers
  185.     and pass any info they may contain to the format function.
  186.  
  187.     format sub_functions must return number words used so can
  188.     update info in words.
  189.  
  190.     update_words must return how many words left.
  191.  
  192.     will continue formating if any words left or
  193.     still input information
  194. */
  195.  
  196. // ----------------------------------------------------- now do work
  197.  
  198. int divide () {
  199.     char    temp [MAX_LINE_LEN];
  200.                     // stores space in case needed
  201.     for (unsigned int ch,i=0; isspace(ch = buffer[i]); i++) {
  202.         if (ch == '\n'|| ch == '\0') {
  203.             Words.newp = 1;
  204.             buffer[0] = '\0';       // ie just space
  205.             return 1;       // 1 means stop adding
  206.             }
  207.         temp[i] = ch;
  208.         }
  209.     if (ch == EOF) {
  210.         Words.end =1;           // must prevent
  211.                         // more input
  212.         Words.newp = 0;
  213.         buffer[0] = '\0';
  214.         return 1;
  215.         }
  216.  
  217.     if (buffer[strlen(buffer)-1] == '\n')
  218.         buffer[strlen(buffer)-1] = '\0';
  219.  
  220.     if (strchr(Fmt.pmarkers,ch)) {      // check if para marker
  221.         strcpy(Words.param, &buffer[i]);
  222.         ch = strlen(Words.param);
  223.         if (Words.param[ch-1] == '\n')
  224.             Words.param[ch-1] = '\0';
  225.         buffer[0] = '\0';
  226.         Words.newp = 1;
  227.         return 1;
  228.         }
  229.     temp[i] = '\0';                         // make sure space
  230.                         // is terminated
  231.     if (Words.newp || Fmt.allpara) strcpy(Words.space, temp);
  232.  
  233. // -------------------------------- extract words
  234.  
  235.     int w_ctr;                              // internal word counter
  236.                         // Words.wnum = #words
  237.                         // j is position marker
  238.  
  239.     for (int j=i; j < strlen(buffer); j++) {
  240.         w_ctr = 0;
  241.         if (buffer[j] == EOF) {
  242.             Words.end = 1;
  243.             break;
  244.             }
  245.         if (isspace(buffer[j])) continue;
  246.                         // ignore space
  247.         while (!isspace(ch=buffer[j])) {
  248.             Words.words [Words.wnum][w_ctr] = ch;
  249.             j++;
  250.             w_ctr++;
  251.             }
  252.                         // one complete word fnd
  253.  
  254.         Words.words [Words.wnum] [w_ctr] = '\0';
  255.  
  256.                         // check if is a sentence
  257.         if (strchr(Fmt.smarkers,buffer[j-1])) {
  258.             Words.words [Words.wnum] [w_ctr] = ' ';
  259.             Words.words [Words.wnum] [w_ctr+1] = '\0';
  260.             }
  261.  
  262.         if (strlen(Words.words[Words.wnum])) Words.wnum++;
  263.  
  264.         if (buffer[j] == '\0' )
  265.             break;
  266.  
  267.         if (buffer[j] == EOF) {
  268.             Words.end = 1;
  269.             break; }
  270.  
  271.         }
  272.     Words.newp = 0;
  273.     strcpy(temp, &buffer[i]);
  274.     strcpy(buffer,temp);
  275.  
  276.     if (Fmt.allpara) return 1;
  277.     if (Words.wnum >= (MAX_NUM_WORDS) - (WNUM_OFFSET))
  278.         return 1;
  279.     return Words.newp;
  280.     }
  281.  
  282. // ------------------------------------------- space calc
  283.                         // assumes tab = 8
  284. int Calc_space_len() {
  285.     int pos = 0;
  286.     for (int i=0, s_p